package com.iemes.controller.suppfille;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;   
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.base.ConstantController;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.MaterFromMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.PurchaseFromMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.MaterMapper;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.mapper.order.OrderMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

import net.sf.json.JSONObject;

@RequestMapping("/supp/")
@Controller
public class SuppSelController extends BaseController {
	@Inject
	private OrderMapper orderMapper;
	@Inject
	private SuppfilleMapper suppMapper;
	@Inject
	private MaterMapper matertMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private QualityMapper qualityMapper;
	
	@RequestMapping("examineOrder")
	public String examineOrder(Model model){
		List<SuppFilleFromMap> suppFormMaps=suppMapper.findByWhere(new SuppFilleFromMap());
		model.addAttribute("client", suppFormMaps);
		List<MaterFromMap> materFormMaps=matertMapper.findByWhere(new MaterFromMap());
		model.addAttribute("mater", materFormMaps);
		List<UserFormMap> userFormMaps=userMapper.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		/*List<GradeFromMap> gardeFromMap=gradeMapper.findByWhere(new GradeFromMap());
		model.addAttribute("grade", gardeFromMap);*/
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/suppfille/suppfille_list";
	}
	
	@ResponseBody
	@RequestMapping("supp_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		SuppFilleFromMap suppFromMap = getFormMap(SuppFilleFromMap.class);
		List<Map<String, Object>> suppFormMaps=suppMapper.getAllName(suppFromMap);
		LuogicPaging(pageNow, pageSize, suppFormMaps);
		pageView.setRecords(suppFormMaps);
		return pageView;
	}
	
	
	@RequestMapping("supp_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<BrandFormMap> brandFormMaps=userMapper.findByWhere(new BrandFormMap());
		model.addAttribute("brand", brandFormMaps);
		if(Common.isNotEmpty(id)){
			SuppFilleFromMap suppCheckFormMap=baseMapper.findbyFrist("id", id, SuppFilleFromMap.class);
			model.addAttribute("before",suppCheckFormMap);
			MaterFromMap materFormMaps=new MaterFromMap();
			materFormMaps.put("sippNumber", suppCheckFormMap.getStr("suppNumber"));
			List<MaterFromMap> materFormMap=baseMapper.findByNames(materFormMaps);
			model.addAttribute("mater", materFormMap);
		}
		return Common.BACKGROUND_PATH + "/suppfille/suppfille_edit";
	}
	@ResponseBody
	@RequestMapping(value="supp_editEntity")
	@SystemLog(module="物料管理",methods="供应商信息-修改表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		SuppFilleFromMap suppCheckFormMap=getFormMap(SuppFilleFromMap.class);
		baseMapper.editEntity(suppCheckFormMap);
		//先删除
		baseMapper.deleteByAttribute("materialNo","'"+suppCheckFormMap.getStr("materialNo")+"'", MaterFromMap.class);
		//baseMapper.deleteByAttribute("materialNo","'"+suppCheckFormMap.getStr("materialNo")+"'", SuppFilleFromMap.class);
		//再添加
		String suppName =request.getParameter("suppFilleFromMap.suppName"); 
		String suppNumber =request.getParameter("suppFilleFromMap.suppNumber");
		String purchaseNo =request.getParameter("suppFilleFromMap.purchaseNo"); 
		String gradeService =request.getParameter("suppFilleFromMap.gradeService");
		String remark =request.getParameter("suppFilleFromMap.remark");
		String[] materialNo = request.getParameterValues("materialNo1");
		String[] sippNumber = request.getParameterValues("sippNumber1");
		String[] productName = request.getParameterValues("productName1");
		String[] materType =request.getParameterValues("materType1"); 
		String[] newprice =request.getParameterValues("newprice1");
		String[] unit = request.getParameterValues("unit1");
		String[] qualityId = request.getParameterValues("qualityId1");
		if(null != materialNo ){
			for (int i = 0; i < materialNo.length; i++) {
				MaterFromMap materFormMap = new MaterFromMap();
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("suppNumber", suppNumber);
				suppFormMap1.put("materialNo", materialNo[i]);
				suppFormMap1.put("newprice",  Double.parseDouble(newprice[i]));
				suppFormMap1.put("purchaseNo", purchaseNo);
				suppFormMap1.put("suppName", suppName);
				suppFormMap1.put("gradeService",Integer.parseInt(gradeService));
				suppFormMap1.put("remark", remark);
				
				materFormMap.put("sippNumber", sippNumber[i]);
				materFormMap.put("materialNo", materialNo[i]);
				materFormMap.put("productName", productName[i]);
				materFormMap.put("materType", materType[i]);
				materFormMap.put("unit", unit[i]);
				materFormMap.put("qualityId",  Integer.parseInt(qualityId[i]));
				baseMapper.addEntity(materFormMap);
				baseMapper.addEntity(suppFormMap1);
			}
		}
		
		return "success";
	}


	@RequestMapping("supp_addUI")
	public String addUI(Model model) throws Exception {
		List<BrandModelFormMap> brandModelFormMaps=userMapper.findByWhere(new BrandModelFormMap());
		model.addAttribute("Model", brandModelFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		return Common.BACKGROUND_PATH + "/suppfille/suppfille_add";
	}
	
	@ResponseBody
	@RequestMapping("supp_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="供应商-新增物料清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		SuppFilleFromMap suppFormMap = getFormMap(SuppFilleFromMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 物料清单子项
		String suppName =request.getParameter("suppFilleFromMap.suppName"); 
		String suppNumber =request.getParameter("suppFilleFromMap.suppNumber");
		String purchaseNo =request.getParameter("suppFilleFromMap.purchaseNo"); 
		String gradeService =request.getParameter("suppFilleFromMap.gradeService");
		String remark =request.getParameter("suppFilleFromMap.remark");
		String[] materialNo = request.getParameterValues("materialNo1");
		String[] sippNumber = request.getParameterValues("sippNumber1");
		String[] productName = request.getParameterValues("productName1");
		String[] materType =request.getParameterValues("materType1"); 
		String[] newprice =request.getParameterValues("newprice1");
		String[] unit = request.getParameterValues("unit1");
		String[] qualityId = request.getParameterValues("qualityId1");
		if(null != materialNo ){
			for (int i = 0; i < materialNo.length; i++) {
				MaterFromMap materFormMap = new MaterFromMap();
				SuppFilleFromMap suppFormMap1 = new SuppFilleFromMap();
				suppFormMap1.put("suppNumber", suppNumber);
				suppFormMap1.put("materialNo", materialNo[i]);
				suppFormMap1.put("newprice",  Double.parseDouble(newprice[i]));
				suppFormMap1.put("purchaseNo", purchaseNo);
				suppFormMap1.put("suppName", suppName);
				suppFormMap1.put("gradeService",Integer.parseInt(gradeService));
				suppFormMap1.put("remark", remark);
				
				materFormMap.put("sippNumber", sippNumber[i]);
				materFormMap.put("materialNo", materialNo[i]);
				materFormMap.put("productName", productName[i]);
				materFormMap.put("materType", materType[i]);
				materFormMap.put("unit", unit[i]);
				materFormMap.put("qualityId",  Integer.parseInt(qualityId[i]));
				baseMapper.addEntity(materFormMap);
				baseMapper.addEntity(suppFormMap1);
			}
		}
		return "success";
	}
	/*@ResponseBody
	@RequestMapping("supp_delEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="生产物料控制",methods="物料维护-删除物料清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			SuppFilleFromMap suppFormMap = baseMapper.findbyFrist("id", id, SuppFilleFromMap.class);
			String materialNo = suppFormMap.getStr("materialNo");
			MaterFromMap materFormMap = new MaterFromMap();
			materFormMap.put("materialNo", "'"+materialNo+"'");
			baseMapper.deleteByNames(materFormMap);
			baseMapper.deleteByAttribute("id", id, SuppFilleFromMap.class);
		}
		return "success";
	}*/
	
	@ResponseBody
	@RequestMapping("supp_delEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="出仓订单-删除出仓清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		SuppFilleFromMap suppFormMap=getFormMap(SuppFilleFromMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for(int i=0;i<str.length;i++){
			suppFormMap.put("date", sdf.format(date));
			suppFormMap.set("status", 0);
			suppFormMap.put("id", str[i]);
			suppMapper.editEntity(suppFormMap);
		}
		return "success";
	}
	
	
	
	
	
}

